﻿//----------------------------------------------------------------------------------------------------
// The InventoryManager is responsible for handling players inventories
//----------------------------------------------------------------------------------------------------

package HG.Managers
{
	import HG.Debug;
	import HG.GameService;
	
	import HG.GameDataTypes.Item;
	
	import HG.Events.InventoryEvent;
	
	public class InventoryManager extends IGameManager
	{
		var m_items:Array;
		
		//----------------------------------------------------------------------------------------------------
		public function InventoryManager()
		{
			m_items = new Array();
		}
			
		//----------------------------------------------------------------------------------------------------
		override public function Initialize()
		{
		}
		
		//----------------------------------------------------------------------------------------------------
		override public function Shutdown()
		{
		}
		
		//----------------------------------------------------------------------------------------------------
		override public function OnTick(DeltaTime:Number)
		{
		}
		
		//----------------------------------------------------------------------------------------------------
		// Events
		//----------------------------------------------------------------------------------------------------
		public function RaiseNewItemEvent(item:Item)
		{
			var evt:InventoryEvent = new InventoryEvent(InventoryEvent.NEW);
			
			evt.m_playerID = item.GetOwner();
			evt.m_itemID = item.GetID();
			
			dispatchEvent(evt);
		}
		
		//----------------------------------------------------------------------------------------------------
		// External hooks
		//----------------------------------------------------------------------------------------------------
		public function AddPlayerEncryptionKey(playerID:Number, encryptionKey:Number)
		{
			Debug.Trace("Got Encryption Key");
			
			var itemID:Number = m_items.length;
			var item:Item = new Item(playerID, Item.TYPE_ENCRYPTION_KEY, itemID);
			
			item.SetContents(encryptionKey);
			
			m_items[itemID] = item;
			
			RaiseNewItemEvent(item);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetPlayerHasEncryptionKey(playerID:Number, encryptionKey:Number):Boolean
		{
			var index:Number = 0;
			var listLength:Number = m_items.length;
			
			for (index = 0; index < listLength; ++index)
			{
				var item:Item = m_items[index];
				
				if (item.GetOwner() != playerID)
					continue;
					
				if (item.GetContents() == encryptionKey)
					return true;
			}
			
			return false;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function RemovePlayerEncryptionKey(playerID:Number, encryptionKey:Number)
		{
		
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetItem(itemID:Number):Item
		{
			return m_items[itemID];
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}





